package com.data.biz.service.impl;

import com.data.biz.constant.FunctionTypeEnum;
import com.data.biz.domain.*;
import com.data.biz.mapper.*;
import com.data.biz.service.IBizFanService;
import com.data.biz.service.IBizSensorCommonService;
import com.data.biz.vo.SensorVo.common.FanGeneralDataVo;
import com.data.biz.vo.SensorVo.point.WindPointVo;
import com.data.common.constant.UserConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BizSensorCommonServiceImpl implements IBizSensorCommonService {

    @Autowired
    private WindPointMapper pointMapper;

    @Autowired
    private BizFanMapper fanMapper;

    @Autowired
    private IBizFanService iBizFanService;

    @Autowired
    private FunctionModelMapper functionModelMapper;

    @Autowired
    private FunctionMapper functionMapper;

    @Autowired
    private TableCountMapper tableCountMapper;

    @Autowired
    private TableCreateMapper tableCreateMapper;

    @Override
    public FanGeneralDataVo getFanGeneralData(Integer fanId) {
        FanGeneralDataVo result = new FanGeneralDataVo();
        Map<String, List<TableCount>> map = new HashMap<>();
        List<Function> list = getFunctionByFanId(fanId);
        for(Function function:list){
            TableCountExample example = new TableCountExample();
            TableCountExample.Criteria criteria = example.createCriteria();
            criteria.andFanIdEqualTo(fanId);
            criteria.andFunctionIdEqualTo(function.getFanId());
            List<TableCount> datalist = tableCountMapper.selectByExample(example);
            map.put(function.getType(),datalist);
        }
        result.setData(map);
        return result;
    }

    @Override
    public String editWindPoint(WindPointVo vo) {
        try {
            WindPoint point = pointMapper.selectByPrimaryKey(vo.getId());
            if (vo.getPointName() != null) {
                point.setPointName(vo.getPointName());
            }
            if (vo.getInit1() != null) {
                point.setInit1(vo.getInit1());
            }

            if (vo.getInit2() != null) {
                point.setInit2(vo.getInit2());
            }
            if (vo.getOverLine1() != null) {
                point.setOverLine1(vo.getOverLine1());
            }
            if (vo.getOverLine2() != null) {
                point.setOverLine2(vo.getOverLine2());
            }
            pointMapper.updateByPrimaryKeySelective(point);
            return "修改成功";
        } catch (Exception e) {
            return e.getMessage();
        }

    }

    @Override
    public List<BizFan> getFanlist(BizFan fan) {
        BizFanExample example = new BizFanExample();
        BizFanExample.Criteria criteria = example.createCriteria();
        if(fan != null){
            if(StringUtils.isNotBlank(fan.getFanName())){
                criteria.andFanCodeLike(fan.getFanName());
            }
            if(fan.getStatus() != null){
                criteria.andStatusEqualTo(fan.getStatus());
            }
        }

        criteria.andStatusNotEqualTo((short)3);
        return fanMapper.selectByExample(example);
    }

    @Override
    public List<Function> selectFunctionList(Integer fanId) {
        List<Function> list = getFunctionByFanId(fanId);
        return list;
    }

    private List<Function> getFunctionByFanId(Integer fanId) {
        FunctionExample example = new FunctionExample();
        FunctionExample.Criteria criteria = example.createCriteria();
        criteria.andFanIdEqualTo(fanId);
        List<Function> list = functionMapper.selectByExample(example);
        return list;
    }


    @Override
    public List<WindPoint> selectPointList(Integer fanId, Integer functionId, String name) {
        List<WindPoint> list = getPointListByEntity(fanId, functionId, name);
        return list;
    }

    private List<WindPoint> getPointListByEntity(Integer fanId, Integer functionId, String name) {
        WindPointExample example = new WindPointExample();
        WindPointExample.Criteria criteria = example.createCriteria();
        if (fanId != null) {
            criteria.andFanIdEqualTo(fanId);
        }
        if (functionId != null) {
            criteria.andFunctionIdEqualTo(functionId);
        }
        if (StringUtils.isNotBlank(name)) {
            criteria.andPointNameLike(name);
        }
        List<WindPoint> list = pointMapper.selectByExample(example);
        return list;
    }


    @Override
    public List<WindPointVo> getWindPointlist(WindPoint point) {
        List<WindPointVo> result = new ArrayList<>();
        WindPointExample example = new WindPointExample();
        WindPointExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(point.getPointName())) {
            criteria.andPointNameLike("%" + point.getPointName() + "%");
        }
        criteria.andIsDeleteEqualTo((short)0);
        List<WindPoint> list =  pointMapper.selectByExample(example);

        Map<Long,BizFan> fanMap = getFanIdMap();

        for(WindPoint point1:list){
            WindPointVo vo = new WindPointVo();
            BeanUtils.copyProperties(point1,vo);
            vo.setFanName(fanMap.get(vo.getFanId().longValue()).getFanName());
            vo.setFunctionName(FunctionTypeEnum.getNameById(vo.getFunctionId()));
            result.add(vo);
        }
        return  result;
    }

    @Override
    public int deletePointByIds(String ids) throws Exception {
        WindPoint point = pointMapper.selectByPrimaryKey(Integer.parseInt(ids));
        point.setIsDelete((short) 1);
        try {
            pointMapper.updateByPrimaryKeySelective(point);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }


    public Map<Long,BizFan> getFanIdMap(){
        List<BizFan> list = getFanlist(null);
        Map<Long,BizFan> map = new HashMap<>();
        for(BizFan fan:list){
            map.put(fan.getId(),fan);
        }
        return map;
    }


    public Map<Integer,WindPoint> getPointIdMap(){
        List<WindPoint> list = getPointListByEntity(null,null,null);
        Map<Integer,WindPoint> map = new HashMap<>();
        for(WindPoint point:list){
            map.put(point.getId(),point);
        }
        return map;
    }

    @Override
    public int deleteFanByIds(String ids) throws Exception {
        BizFan fan = fanMapper.selectByPrimaryKey(Long.valueOf(ids));
        fan.setStatus((short)3);
        try {
            fanMapper.updateByPrimaryKeySelective(fan);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }




    @Override
    public int insertPoint(WindPoint point) throws Exception {
        String tableName = getTableByPoint(point);
        int tableExist = tableCreateMapper.checkIfTableExist(tableName);
        if(tableExist == 0){
            if(point.getFunctionType().equals("DIP")){
                tableCreateMapper.createDipTable(tableName);
            }
            if(point.getFunctionType().equals("ACC")){
                tableCreateMapper.createAccTable(tableName);
            }
        }
        point.setCreateTime(new Date());
        point.setInitTime(point.getInitTime());
        int id = pointMapper.insert(point);
        TableCount count = new TableCount();
        count.setPointId(id);
        count.setFunctionId(point.getFunctionId());
        count.setIsDelete((short)0);
        count.setFanId(point.getFanId());
        count.setFunctionType(point.getFunctionType());
        count.setPointName(point.getPointName());
        tableCountMapper.insert(count);
        return 0;
    }

    @Override
    public int insertFan(BizFan fan) throws Exception {
        return fanMapper.insert(fan);
    }

    @Override
    public WindPoint selectPointById(Integer id) {
        return pointMapper.selectByPrimaryKey(id);
    }

    @Override
    public BizFan selectFanById(Integer id) {
        return fanMapper.selectByPrimaryKey(id.longValue());
    }

    @Override
    public int updatePoint(WindPoint point) {
        String tableName = getTableByPoint(point);
        int tableExist = tableCreateMapper.checkIfTableExist(tableName);
        if(tableExist == 0){
            if(point.getFunctionType().equals("DIP")){
                tableCreateMapper.createDipTable(tableName);
            }
            if(point.getFunctionType().equals("ACC")){
                tableCreateMapper.createAccTable(tableName);
            }
        }
        return pointMapper.updateByPrimaryKeySelective(point);
    }

    public String getTableByPoint(WindPoint point) {
        String tableName = point.getIp() + "_" + FunctionTypeEnum.getIndexById(point.getFunctionId()).toLowerCase(Locale.ROOT) + "_" + point.getCom();

        String name = tableName.replace('.', '_');
        return name;
    }

    @Override
    public int updateFan(BizFan fan) {
        return fanMapper.updateByPrimaryKeySelective(fan);
    }

    @Override
    public String checkPointNameUnique(WindPoint point) {
//        WindPointExample example = new WindPointExample();
//        WindPointExample.Criteria criteria = example.createCriteria();
//        if (StringUtils.isNotBlank(point.getPointName())) {
//            criteria.andPointNameEqualTo(point.getPointName());
//        }
//        if (pointMapper.selectByExample(example) != null && pointMapper.selectByExample(example).size() != 0) {
//            return UserConstants.POINT_NAME_NOT_UNIQUE;
//        }
//        return UserConstants.POINT_NAME_UNIQUE;
        return UserConstants.POINT_NAME_UNIQUE;

    }


    @Override
    public String checkFanNameUnique(BizFan fan) {
//        BizFanExample example = new BizFanExample();
//        BizFanExample.Criteria criteria = example.createCriteria();
//        if (StringUtils.isNotBlank(fan.getFanName())) {
//            criteria.andFanNameEqualTo(fan.getFanName());
//        }
//        if (fanMapper.selectByExample(example) != null && fanMapper.selectByExample(example).size() != 0) {
//            return UserConstants.FAN_NAME_NOT_UNIQUE;
//        }
//        return UserConstants.FAN_NAME_UNIQUE;
        return UserConstants.FAN_NAME_UNIQUE;

    }


}
